﻿using ApiDoc.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls;

namespace ApiDoc.Admin.View.Folder
{
    /// <summary>
    /// frmFolder.xaml 的交互逻辑
    /// </summary>
    public partial class frmFolder : BasePage
    {
        private HttpClientApi httpClient;
       

        public frmFolder()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            this.httpClient = new HttpClientApi();
            LoadDataAll();
        }


        public override void CallBack(object model)
        {
            base.CallBack(model);

            FolderModel folder = (FolderModel)model;
            TreeViewItem treeViewItem = (TreeViewItem)this.tvFolder.SelectedItem;
            switch (doType)
            {
                case PageDoType.Add:
                    AddTreeNode(folder);
                    break;
                case PageDoType.Update:
                    treeViewItem.DataContext = model;
                    treeViewItem.Header = folder.FolderName;
                    break;
            }
        }

        private async void LoadData()
        { 
            int parentSN = 0;
            ItemCollection Items = this.tvFolder.Items;
            if (this.tvFolder.SelectedItem != null)
            {
                TreeViewItem tviRoot = (TreeViewItem)this.tvFolder.SelectedItem;
                parentSN = ((FolderModel)tviRoot.DataContext).SN;
                Items = tviRoot.Items;
            }

            List<FolderModel> folders = await this.httpClient.FolderList(parentSN);

            Items.Clear();
            foreach (FolderModel folder in folders)
            {
                AddTreeNode(folder);
            }
        }

        private async void LoadDataAll()
        { 
            List<FolderModel> folders = await this.httpClient.FolderListAll();

            this.tvFolder.Items.Clear();

            AddChildNode(0, folders, this.tvFolder.Items);
        }

        private void AddChildNode(int ParentSN, List<FolderModel> folders, ItemCollection Items)
        {
            foreach (FolderModel folder in folders.FindAll(oo => oo.ParentSN == ParentSN))
            {
                WQDTreeViewItem tviChild = CreateTreeViewItem(folder);
                Items.Add(tviChild);

                AddChildNode(folder.SN, folders, tviChild.Items);
            }
        }

        private void AddTreeNode(FolderModel folder)
        {
            ItemCollection Items = this.tvFolder.Items;
            if (this.tvFolder.SelectedItem != null)
            {
                TreeViewItem tviRoot = (TreeViewItem)this.tvFolder.SelectedItem; 
                Items = tviRoot.Items;
            }

            WQDTreeViewItem treeViewItem = CreateTreeViewItem(folder);
            Items.Add(treeViewItem);
        }

        private WQDTreeViewItem CreateTreeViewItem(FolderModel folder)
        {
            WQDTreeViewItem treeViewItem = new WQDTreeViewItem();
            treeViewItem.DataContext = folder;
            treeViewItem.Header = folder.FolderName;
            return treeViewItem;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            doType = PageDoType.Add;
            string parentId = "";

            if (this.tvFolder.SelectedItem != null)
            {
                parentId =((FolderModel)((WQDTreeViewItem)this.tvFolder.SelectedItem).DataContext).Id;
            }

            FolderModel model = new FolderModel();
            model.ParentSN = SelectFolderModel.SN;
            model.Id = parentId + DateTimeUtil.DateTimeToTimeStamp(DateTime.Now).ToString();

            base.Contain.Navigate("/View/Folder/frmFolderEdit", model); 
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            doType = PageDoType.Update;
            base.Contain.Navigate("/View/Folder/frmFolderEdit", SelectFolderModel); 
        }

        private void btnRefaush_Click(object sender, RoutedEventArgs e)
        {
            this.LoadData();
        }

        private void tvFolder_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = (TreeViewItem)this.tvFolder.SelectedItem;
            if (treeViewItem != null && treeViewItem.Items.Count == 0)
            {
                this.LoadData();
            }
        }

        private async void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("请确认是否要删除") == MessageBoxResult.OK)
            {
                if (this.tvFolder.SelectedItem != null)
                {
                    WQDTreeViewItem tviSelected = (WQDTreeViewItem)this.tvFolder.SelectedItem;
                    ItemCollection Items = tviSelected.Items;
                    if (tviSelected.Parent == null)
                    {
                        Items = this.tvFolder.Items;
                    }
                    else
                    {
                        Items = ((WQDTreeViewItem)tviSelected.Parent).Items;
                    }
                    int SN = ((FolderModel)tviSelected.DataContext).SN;
                    int result = await this.httpClient.FolderDelete(SN);
                    if (result > 0)
                    {
                        Items.Remove(this.tvFolder.SelectedItem);
                    }
                }
            }
        }

        private FolderModel SelectFolderModel
        {
            get
            {
                if (this.tvFolder.SelectedItem != null)
                {
                   return (FolderModel)((TreeViewItem)this.tvFolder.SelectedItem).DataContext;
                }

                return new FolderModel();
            }
        }
    }
}
